<?php

namespace app\http\common\controller;

use support\Db;
use support\Request;

/**
 * @desc    生成表属性
 * @author  OverMiracle
 * @date    2023-12-10
 */
class Table
{
    public function index(Request $request, string $name = ''): string
    {
        if ($name == '') return '表名不能为空，请求url为/table/表名称';
        $comment = '';
        $sql = "SHOW FULL COLUMNS FROM {$name}";
        $columns = DB::select($sql);

        if (empty($columns)) return $comment;
        foreach ($columns as $item) {
            $type = 'mixed';
            if (str_contains($item->Type, 'char')) {
                $type = 'string';
            }
            if (str_contains($item->Type, 'varchar')) {
                $type = 'string';
            }
            if (str_contains($item->Type, 'text')) {
                $type = 'string';
            }
            if (str_contains($item->Type, 'date')) {
                $type = 'string';
            }
            if (str_contains($item->Type, 'int')) {
                $type = 'int';
            }
            if (str_contains($item->Type, 'decimal')) {
                $type = 'double';
            }
            if (str_contains($item->Type, 'double')) {
                $type = 'double';
            }
            if (str_contains($item->Type, 'float')) {
                $type = 'double';
            }
            $comment .= '* @property ' . $type . ' $' . $item->Field . ' ' . $item->Comment . "<br>";
        }
        return $comment;
    }
}